﻿using System;
using System.Runtime.CompilerServices;

namespace Common.Msg
{
    /// <summary>
    /// 记录系统日志公用类
    /// 默认只开启错误
    /// 开启Info日志记录请配置InfoLog为true
    /// 开启Warn日志记录请配置WarnLog为true
    /// </summary>
    internal class Logger : ILogger
    {
        public string AppName { get; private set; }

        public string AppVersion { get; private set; }

        public string ServerName { get; private set; }

        private static log4net.ILog log;

        public void InitConfig(string configContent, string logName = "", string appName = "", string appVersion = "", string serverName = "")
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.LoadXml(configContent);
            log4net.Config.XmlConfigurator.Configure(doc.DocumentElement);
            if (string.IsNullOrEmpty(logName) == false)
                log = log4net.LogManager.GetLogger(logName);

            SetServiceInfo(appName, appVersion, serverName);
        }

        public void InitConfigByFile(string configPath, string logName = "", string appName = "", string appVersion = "", string serverName = "")
        {
            //string baseDir = System.AppDomain.CurrentDomain.BaseDirectory;
            //System.IO.FileInfo file = new System.IO.FileInfo(System.IO.Path.Combine(baseDir, "log4net.config"));
            System.IO.FileInfo file = new System.IO.FileInfo(configPath);
            log4net.Config.XmlConfigurator.Configure(file);
            if (string.IsNullOrEmpty(logName) == false)
                log = log4net.LogManager.GetLogger(logName);

            SetServiceInfo(appName, appVersion, serverName);
        }

        private void SetServiceInfo(string appName , string appVersion , string serverName)
        {
            this.AppName = appName;
            this.AppVersion = appVersion;
            this.ServerName = serverName;
        }


        public void Debug(string message)
        {
            Debug(message, null, null);
        }
        public void Debug(string message, Exception exception)
        {
            Debug(message, null, exception);
        }
        public void Debug(string message, object Data, Exception exception = null)
        {
            if (log.IsDebugEnabled)
            {
                log.Debug(GetLogMessage("Debug", message, Data, exception));
            }
        }



        public void Error(string message)
        {
            Error(message, null, null);
        }
        public void Error(string message, Exception exception)
        {
            Error(message, null, exception);
        }
        public void Error(string message, object Data, Exception exception = null)
        {
            if (log.IsErrorEnabled)
            {
                log.Error(GetLogMessage("Error", message, Data, exception));
            }
        }



        public void Fatal(string message)
        {
            Fatal(message, null, null);
        }
        public void Fatal(string message, Exception exception)
        {
            Fatal(message, null, exception);
        }
        public void Fatal(string message, object Data, Exception exception = null)
        {
            if (log.IsFatalEnabled)
            {
                log.Fatal(GetLogMessage("Fatal", message, Data, exception));
            }
        }



        public void Info(string message)
        {
            Info(message, null, null);
        }
        public void Info(string message, Exception exception)
        {
            Info(message, null, exception);
        }
        public void Info(string message, object Data, Exception exception = null)
        {
            if (log.IsInfoEnabled)
            {
                log.Info(GetLogMessage("Info", message, Data, exception));
            }
        }



        public void Warn(string message)
        {
            Warn(message, null, null);
        }
        public void Warn(string message, Exception exception)
        {
            Warn(message, null, exception);
        }
        public void Warn(string message, object Data, Exception exception = null)
        {
            if (log.IsWarnEnabled)
            {
                log.Warn(GetLogMessage("Warn", message, Data, exception));
            }
        }



        public object GetLogMessage(string logLevel, string message, object Data, Exception exception)
        {

            var logObj = new
            {
                AppName = this.AppName,
                AppVersion = this.AppVersion,
                ServerName = this.ServerName,
                Time = DateTime.Now,
                TimeStamp = DateTime.Now.Ticks,
                Logger = log.Logger.Name,
                Level = logLevel,
                Message = message,
                ExtraData = Data,
                Exception = ((exception == null) ? null : exception.Message),
                StackTrace = ((exception == null) ? null : exception.StackTrace)
            };
            return Serialize.JsonHelper.ToJson(logObj);
        }
    }
}
